<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @abstract 配件处理API
 * @author zxj
 */
class OrderParts extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('fee_model','feeModel');
    }

    /**
     * 处理配件状态接口
     */
    function requestParts()
    {
        $requestData = gp();
        log_output($requestData);
        $token = $requestData['token'];
        $id = (int)$requestData['id'];
        if ($token != '78de933bd0fbacf02ef2de1116e3338f1c627b07') {
            json_output('', 'token错误，无效的token', false);
        }
        $partsFirst = $this->common_model->selectFirst('mt_product_parts', array('id' => $id));
        $inData = array();
        if (!$partsFirst) {
            json_output($requestData, '该前配件不存在', false);
        } else if ($partsFirst['status'] == 1) {
            json_output('', '该配件出于正常状态,操作失败', false);
        } else if ($partsFirst['status'] == 2) {
            if (!$requestData['sku'] || !$requestData['price']) {
                json_output($requestData, '该配件没有价格及SKU,报价失败', false);
            }
            //#价格处理
            $inData['rate_price'] = $requestData['price'];
            if ($requestData['price'] > 0 && $requestData['currency'] != $partsFirst['rate_currency']) {
                $inData['rate_price'] = currency_service($requestData['currency'], $partsFirst['rate_currency'], $requestData['price']);
            }
            $inData['sku'] = $requestData['sku'];
            $inData['price'] = $requestData['price'];
            $inData['cost_price'] = $requestData['cost_price'];
            $inData['currency'] = $requestData['currency'];
            $inData['status'] = 3;
            $inData['delayData'] = $requestData['date'];
            $inData['sap_status'] = $requestData['status'];
            $this->common_model->update('mt_product_parts', $inData, array('id' => $id));
        } else {
            $upInData['delayData'] = $requestData['date'];
            $upInData['sap_status'] = $requestData['status'];
            if($requestData['status']==12) {
                $upInData['status'] = 1;
            }
            $this->common_model->update('mt_product_parts', $upInData, array('id' => $id));
        }
        //#更新状态
        if($partsFirst['status']!=3){
            $this->common_model->resetStatus($partsFirst['pid']);
        }
        if($partsFirst['status']==2){
            $this->feeModel->setProductPrice($partsFirst['pid']);
        }
        json_output('', 'succeed', true);
    }

    /**
     * WMS 配件对接
     */
	function requestParts4Wms()
    {
        $requestData = gp();
        log_output($requestData);
        $token = $requestData['token'];
        $id = (int)$requestData['id'];
        $status = (int)$requestData['status'];
        $dhCount = (int)$requestData['dhCount'];
        if ($token != '78de933bd0fbacf02ef2de1116e3338f1c627b07') {
            json_output('', 'token错误，无效的token', false);
        }
        $partsFirst = $this->common_model->selectFirst('mt_product_parts', array('id' => $id));
        $inData = array();
        if (!$partsFirst) {
            json_output($requestData, '该前配件不存在', false);
        } else if ($partsFirst['status'] == 1) {
            if($status == 5){
                $this->common_model->update('mt_product_parts',array('status'=>5),array('id'=>$id));
            }else{
                json_output($requestData, '该配件处于正常状态', false);
            }
        } else if ($partsFirst['status'] == 2) {
            if (!$requestData['sku'] || !$requestData['price']) {
                json_output($requestData, '该配件没有价格及SKU,报价失败', false);
            }
        } else if($partsFirst['status'] == 3){
            //配件已到货
            $this->common_model->updateset('mt_product_parts', array('status'=>1,'actual_quantity'=>'actual_quantity+'.$dhCount), array('id' => $id));
        }
        if($status == 4){
            $this->common_model->update('mt_product_parts', array('status'=>4), array('id' => $id));
        }
        //#更新状态
        if($partsFirst['status']!=3){
            $this->common_model->resetStatus($partsFirst['pid']);
        }
        json_output('', 'succeed', true);
    }

    /**
     * 配件到货
     * 传入数组array('id'=>'配件ID','arrival_quantity'=>'到货数量');
     */
    public function partsArrival(){
        $gp=gp();
        $result=$this->common_model->updateset('mt_product_parts',array('actual_quantity'=>'actual_quantity+'.$gp['arrival_quantity']),array('id'=>$gp['id']));
        if($result){
            $partsFirst = $this->common_model->selectFirst('mt_product_parts', array('id' => $gp['id']));
            if($partsFirst['quantity'] == $partsFirst['actual_quantity']){
                $this->common_model->update('mt_product_parts',array('status'=>1),array('id' => $gp['id']));
            }
        }
    }

    /**
     * 配件出库
     * 传入数组 array('pid'=>'产品ID')
     */
    public function partsOutput(){
        $gp=gp();
        $result=$this->common_model->update('mt_product_parts',array('status'=>5),array('pid' => $gp['pid']));
        $product_status = $this->common_model->selectFirst('mt_product', array('id' => $gp['pid']));
        $user = $this->common_model->selectFirst('mt_product_track', array('end_sid' => 8, 'pid' => $gp['pid']));
        $uid = $user['uid'];
        $add_user = $user['add_user'];

        if (empty($user['uid']) && empty($user['add_user'])) {
            $uid = 0;
            $add_user = 'no user';
        } 
	 $ststus = $product_status['status_id'];
        if ($result) {
            if ($ststus == 8) {//状态8为“维修中-等待配件”，这里调用的时候是为了使WMS配件出库之后，产品状态自动修改为“维修中”
                $this->common_model->resetStatus($gp['pid'], array('uid' => $uid, 'add_user' => $add_user));
            }
        }
    }

    /**
     * 配件无法采购
     * 传入数组 array('id'=>'配件ID','pid'=>'产品ID')
     */
    public function partsUnableToPurchase(){
        $gp=gp();
        $result=$this->common_model->update('mt_product_parts',array('sap_status'=>7),array('id' => $gp['id']));
        if($result){
            $data=array(
                'available'=>2,
                'hold_remark'=>'配件无法采购',
                'hold_type'=>3
            );
            $this->common_model->update('mt_product',$data,array('id' => $gp['pid']));
        }
    }

}
